path_proj = here::here()
path_source = file.path(path_proj, "source")

source(file.path(path_source, "simulation", "simulations_functions_final.R"))
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:reshape2':
## 
##     smiths
source(file.path(path_source, "functions", "plot_function.R"))
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
source(file.path(path_source, "functions", "fit_function.R"))
source(file.path(path_source, "functions", "table_function.R"))

# place for draws
# mac
posterior_draws_path = file.path(Sys.getenv("HOME"), "Desktop", "draws", "testEach")

#Windows
#posterior_draws_path = file.path(Sys.getenv("USERPROFILE"), "Desktop", "draws", "testEach")

#data path
data_save_path = file.path(path_proj, "data", "fitted_model", "simulation", "3. b_rw")
#models
q_constant <- file.path(path_proj, "source", "models", 
                      "q-constant.stan")
b_constant <- file.path(path_proj, "source", "models", 
                     "b-constant.stan")
b_rw <- file.path(path_proj, "source", "models", 
                     "b-rw1.stan")
b_ou <-  file.path(path_proj, "source", "models", 
                      "b-ou.stan")

compiled_models <- list(
  q_constant = cmdstan_model(q_constant),
  b_constant = cmdstan_model(b_constant),
  b_rw = cmdstan_model(b_rw),
  b_ou = cmdstan_model(b_ou)
)

models_to_use <- c("q_constant", "b_constant", "b_rw", "b_ou")

simulate data - FR

setting

###### setting #####
seed <- 123
set.seed(seed)

# data
alpha_increase_seq_1 <- seq(10, 750, by = 30)
alpha_decrease_seq_1 <- seq(750, 10, by = -30)
alpha_lamb =  c( rep(10,5), alpha_increase_seq_1 + rnorm(alpha_increase_seq_1,10,10), 
                 alpha_decrease_seq_1 + rnorm(alpha_decrease_seq_1,10,10),
                 rep(10,5))
beta_lamb = 0.5
T = 60
# reprot delay
D <- 15;

# Time period for checking
D_check <- 5

first_date <- as.Date("2024-01-01")

scoreRange <- c(first_date+days(9), first_date+days(19), first_date+days(29),
                first_date+days(39), first_date+days(49))
params_b_rw_FR <- list(
  data = list(
    alpha_lamb = alpha_lamb,
    beta_lamb  = beta_lamb,
    T       = T,
    date_start = as.Date("2024-01-01"),
    D = D
  ),
  q_model = list(
    method        = "b_rw",
    method_params = list(mu_logb = log(0.7), sigma_logb = 0.1, mu_logitphi = 1, sigma_logitphi = 0.1)
  )
)

b_rw_FR <- simulateData(params_b_rw_FR)
par(mfrow = c(2, 1))
plot(b_rw_FR$b, pch = 19, type = "b")
plot(b_rw_FR$phi, pch = 19, type = "b")

par(mfrow = c(1, 1))
matplot(t(b_rw_FR$q), type = "l", lty = 1, ylim = c(0, 1))

params_b_rw_NFR <- list(
  data = list(
    alpha_lamb = alpha_lamb,
    beta_lamb  = beta_lamb,
    T       = T,
    date_start = as.Date("2024-01-01"),
    D = D
  ),
  q_model = list(
    method        = "b_rw",
    method_params = list(mu_logb = log(0.1), sigma_logb = 0.1, mu_logitphi = 1.5,
            sigma_logitphi = 0.1)
  )
)

b_rw_NFR <- simulateData(params_b_rw_NFR)
par(mfrow = c(2, 1))
plot(b_rw_NFR$b, pch = 19, type = "b")
plot(b_rw_NFR$phi, pch = 19, type = "b")

par(mfrow = c(1, 1))
matplot(t(b_rw_NFR$q), type = "l", lty = 1, ylim = c(0, 1))

b_rw_NFR$case_true - b_rw_NFR$case_reported_cumulated[,16]
##            [,1]
## 2024-01-01    7
## 2024-01-02    2
## 2024-01-03    6
## 2024-01-04    4
## 2024-01-05    6
## 2024-01-06    6
## 2024-01-07   20
## 2024-01-08   22
## 2024-01-09   27
## 2024-01-10   46
## 2024-01-11   54
## 2024-01-12   38
## 2024-01-13   97
## 2024-01-14  112
## 2024-01-15  122
## 2024-01-16  131
## 2024-01-17  131
## 2024-01-18  139
## 2024-01-19   87
## 2024-01-20  101
## 2024-01-21  136
## 2024-01-22  116
## 2024-01-23  138
## 2024-01-24  158
## 2024-01-25  190
## 2024-01-26  165
## 2024-01-27  232
## 2024-01-28  248
## 2024-01-29  309
## 2024-01-30  332
## 2024-01-31  298
## 2024-02-01  213
## 2024-02-02  218
## 2024-02-03  234
## 2024-02-04  257
## 2024-02-05  227
## 2024-02-06  207
## 2024-02-07  185
## 2024-02-08  183
## 2024-02-09  213
## 2024-02-10  179
## 2024-02-11  161
## 2024-02-12  135
## 2024-02-13  123
## 2024-02-14  127
## 2024-02-15   95
## 2024-02-16  117
## 2024-02-17   75
## 2024-02-18   67
## 2024-02-19   49
## 2024-02-20   37
## 2024-02-21   40
## 2024-02-22   39
## 2024-02-23   32
## 2024-02-24   15
## 2024-02-25    8
## 2024-02-26   15
## 2024-02-27    6
## 2024-02-28    8
## 2024-02-29    5

expplot

# exploritary analysis
page_num <- ceiling(nrow(b_rw_FR$case_reported_cumulated)/16)
exp_plot_b_rw <- fit_exp_plot(b_rw_FR$case_reported_cumulated,ncol = 4, nrow = 4, page = c(1:page_num), if_fit = T)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
print(exp_plot_b_rw)
## $plots
## $plots[[1]]

## 
## $plots[[2]]

## 
## $plots[[3]]

## 
## $plots[[4]]

## 
## 
## $coefficients
##            b       phi
## 1  0.5102478 0.3242183
## 2  0.3653369 0.3084090
## 3  0.4476095 0.3421965
## 4  0.4490649 0.2918580
## 5  0.3397700 0.2939834
## 6  0.6011245 0.3748400
## 7  0.4381524 0.2885234
## 8  0.5476037 0.2856854
## 9  0.5017568 0.3719762
## 10 0.5369134 0.2670427
## 11 0.5729134 0.2276394
## 12 0.5371611 0.2855857
## 13 0.5941961 0.2708847
## 14 0.4387457 0.2990528
## 15 0.7278341 0.2707825
## 16 0.6597422 0.2946573
## 17 0.6511569 0.2539173
## 18 0.6853104 0.2809225
## 19 0.6583041 0.2745087
## 20 0.7350582 0.2385135
## 21 0.7132630 0.2361698
## 22 0.7545682 0.2105783
## 23 0.7576714 0.2360144
## 24 0.7388811 0.2399718
## 25 0.9318239 0.2407887
## 26 0.8858472 0.2032041
## 27 0.7703753 0.2613836
## 28 0.7229013 0.2708974
## 29 0.7746562 0.2797567
## 30 0.8035316 0.2488242
## 31 0.8080676 0.2804276
## 32 0.7148676 0.2288080
## 33 0.8190414 0.2289338
## 34 0.7366797 0.1885100
## 35 0.7052928 0.1987711
## 36 0.7118669 0.1923962
## 37 0.7637729 0.2392929
## 38 0.7788860 0.2183033
## 39 0.7442910 0.2543408
## 40 0.8062958 0.2661258
## 41 0.7907656 0.2748617
## 42 0.8044802 0.2104328
## 43 0.7381683 0.2923139
## 44 0.8673423 0.3002924
## 45 0.7035423 0.2677609
## 46 0.6335863 0.3116783
## 47 0.7580283 0.3187835
## 48 0.8201927 0.2894523
## 49 0.6957779 0.2782749
## 50 0.6578685 0.3586028
## 51 0.6607782 0.3176169
## 52 0.8178736 0.2767404
## 53 0.9253342 0.2464679
## 54 0.8543887 0.2472600
## 55 0.7458717 0.1894083
## 56 0.6579016 0.1592701
## 57 1.0154663 0.1695938
## 58 1.2701982 0.3523995
## 59 0.9695121 0.2565201
## 60 0.7668025 0.3984388
exp_plot_b_rw$coefficients
##            b       phi
## 1  0.5102478 0.3242183
## 2  0.3653369 0.3084090
## 3  0.4476095 0.3421965
## 4  0.4490649 0.2918580
## 5  0.3397700 0.2939834
## 6  0.6011245 0.3748400
## 7  0.4381524 0.2885234
## 8  0.5476037 0.2856854
## 9  0.5017568 0.3719762
## 10 0.5369134 0.2670427
## 11 0.5729134 0.2276394
## 12 0.5371611 0.2855857
## 13 0.5941961 0.2708847
## 14 0.4387457 0.2990528
## 15 0.7278341 0.2707825
## 16 0.6597422 0.2946573
## 17 0.6511569 0.2539173
## 18 0.6853104 0.2809225
## 19 0.6583041 0.2745087
## 20 0.7350582 0.2385135
## 21 0.7132630 0.2361698
## 22 0.7545682 0.2105783
## 23 0.7576714 0.2360144
## 24 0.7388811 0.2399718
## 25 0.9318239 0.2407887
## 26 0.8858472 0.2032041
## 27 0.7703753 0.2613836
## 28 0.7229013 0.2708974
## 29 0.7746562 0.2797567
## 30 0.8035316 0.2488242
## 31 0.8080676 0.2804276
## 32 0.7148676 0.2288080
## 33 0.8190414 0.2289338
## 34 0.7366797 0.1885100
## 35 0.7052928 0.1987711
## 36 0.7118669 0.1923962
## 37 0.7637729 0.2392929
## 38 0.7788860 0.2183033
## 39 0.7442910 0.2543408
## 40 0.8062958 0.2661258
## 41 0.7907656 0.2748617
## 42 0.8044802 0.2104328
## 43 0.7381683 0.2923139
## 44 0.8673423 0.3002924
## 45 0.7035423 0.2677609
## 46 0.6335863 0.3116783
## 47 0.7580283 0.3187835
## 48 0.8201927 0.2894523
## 49 0.6957779 0.2782749
## 50 0.6578685 0.3586028
## 51 0.6607782 0.3176169
## 52 0.8178736 0.2767404
## 53 0.9253342 0.2464679
## 54 0.8543887 0.2472600
## 55 0.7458717 0.1894083
## 56 0.6579016 0.1592701
## 57 1.0154663 0.1695938
## 58 1.2701982 0.3523995
## 59 0.9695121 0.2565201
## 60 0.7668025 0.3984388
exp_b_data_b_rw<- data.frame( date = as.Date(rownames(b_rw_FR$case_reported_cumulated)),
                          b = exp_plot_b_rw$coefficients$b)

exp_b_plot_b_rw <- ggplot(exp_b_data_b_rw, aes(x = date, y = b)) +
  geom_point(color = "black", size = 1.5) +       
  geom_smooth(method = "loess", se = TRUE,        
              color = "blue", fill = "grey", alpha = 0.5) +
  theme_minimal() +
  labs(x = NULL, y = "Y", title = "Smoothed Curve of parameter b")

print(exp_b_plot_b_rw)
## `geom_smooth()` using formula = 'y ~ x'

# exploritary analysis
page_num <- ceiling(nrow(b_rw_NFR$case_reported_cumulated)/16)
exp_plot_b_rw <- fit_exp_plot(b_rw_NFR$case_reported_cumulated,ncol = 4, nrow = 4, page = c(1:page_num), if_fit = T)
print(exp_plot_b_rw)
## $plots
## $plots[[1]]

## 
## $plots[[2]]

## 
## $plots[[3]]

## 
## $plots[[4]]

## 
## 
## $coefficients
##             b        phi
## 1  0.23910127 0.12731033
## 2  0.14307222 0.21207681
## 3  0.18282214 0.12125292
## 4  0.15070194 0.19235510
## 5  0.11672660 0.06986817
## 6  0.19065752 0.17170494
## 7  0.17034141 0.13607374
## 8  0.19247986 0.12220650
## 9  0.17726165 0.09267310
## 10 0.18056943 0.12851609
## 11 0.17190178 0.09483770
## 12 0.18583349 0.11371414
## 13 0.17700723 0.12314865
## 14 0.15689921 0.16709594
## 15 0.14470621 0.11190978
## 16 0.17416584 0.14863184
## 17 0.16389252 0.14095565
## 18 0.15679565 0.17812054
## 19 0.18654569 0.15977487
## 20 0.18081863 0.18148340
## 21 0.18161363 0.21429750
## 22 0.17699920 0.24996216
## 23 0.16964826 0.27218221
## 24 0.17033615 0.25333376
## 25 0.16720046 0.20141935
## 26 0.18549233 0.21639904
## 27 0.16696982 0.19372895
## 28 0.17166586 0.18913271
## 29 0.16957894 0.18463813
## 30 0.15138569 0.19223931
## 31 0.16359092 0.19633644
## 32 0.17575780 0.18140011
## 33 0.17005360 0.18259659
## 34 0.16321524 0.15329901
## 35 0.16124535 0.20477246
## 36 0.16082623 0.24208419
## 37 0.17004578 0.19251882
## 38 0.17189117 0.22746632
## 39 0.16578062 0.21047873
## 40 0.16699487 0.21502090
## 41 0.15850437 0.19739285
## 42 0.16961976 0.22191056
## 43 0.17177557 0.23944822
## 44 0.17098530 0.22533894
## 45 0.16254020 0.21983684
## 46 0.17096671 0.21714553
## 47 0.15942756 0.27243277
## 48 0.17190734 0.22264551
## 49 0.18305043 0.24037747
## 50 0.17497658 0.21235222
## 51 0.16323330 0.28818886
## 52 0.18255363 0.25834212
## 53 0.16006506 0.32805788
## 54 0.18024980 0.26376189
## 55 0.21130160 0.33282115
## 56 0.12760549 0.18362417
## 57 0.17370331 0.07956690
## 58 0.15300945 0.29171182
## 59 0.08145321 0.09746539
## 60 0.17688469 0.18686621
exp_plot_b_rw$coefficients
##             b        phi
## 1  0.23910127 0.12731033
## 2  0.14307222 0.21207681
## 3  0.18282214 0.12125292
## 4  0.15070194 0.19235510
## 5  0.11672660 0.06986817
## 6  0.19065752 0.17170494
## 7  0.17034141 0.13607374
## 8  0.19247986 0.12220650
## 9  0.17726165 0.09267310
## 10 0.18056943 0.12851609
## 11 0.17190178 0.09483770
## 12 0.18583349 0.11371414
## 13 0.17700723 0.12314865
## 14 0.15689921 0.16709594
## 15 0.14470621 0.11190978
## 16 0.17416584 0.14863184
## 17 0.16389252 0.14095565
## 18 0.15679565 0.17812054
## 19 0.18654569 0.15977487
## 20 0.18081863 0.18148340
## 21 0.18161363 0.21429750
## 22 0.17699920 0.24996216
## 23 0.16964826 0.27218221
## 24 0.17033615 0.25333376
## 25 0.16720046 0.20141935
## 26 0.18549233 0.21639904
## 27 0.16696982 0.19372895
## 28 0.17166586 0.18913271
## 29 0.16957894 0.18463813
## 30 0.15138569 0.19223931
## 31 0.16359092 0.19633644
## 32 0.17575780 0.18140011
## 33 0.17005360 0.18259659
## 34 0.16321524 0.15329901
## 35 0.16124535 0.20477246
## 36 0.16082623 0.24208419
## 37 0.17004578 0.19251882
## 38 0.17189117 0.22746632
## 39 0.16578062 0.21047873
## 40 0.16699487 0.21502090
## 41 0.15850437 0.19739285
## 42 0.16961976 0.22191056
## 43 0.17177557 0.23944822
## 44 0.17098530 0.22533894
## 45 0.16254020 0.21983684
## 46 0.17096671 0.21714553
## 47 0.15942756 0.27243277
## 48 0.17190734 0.22264551
## 49 0.18305043 0.24037747
## 50 0.17497658 0.21235222
## 51 0.16323330 0.28818886
## 52 0.18255363 0.25834212
## 53 0.16006506 0.32805788
## 54 0.18024980 0.26376189
## 55 0.21130160 0.33282115
## 56 0.12760549 0.18362417
## 57 0.17370331 0.07956690
## 58 0.15300945 0.29171182
## 59 0.08145321 0.09746539
## 60 0.17688469 0.18686621
exp_b_data_b_rw<- data.frame( date = as.Date(rownames(b_rw_NFR$case_reported_cumulated)),
                          b = exp_plot_b_rw$coefficients$b)

exp_b_plot_b_rw <- ggplot(exp_b_data_b_rw, aes(x = date, y = b)) +
  geom_point(color = "black", size = 1.5) +       
  geom_smooth(method = "loess", se = TRUE,        
              color = "blue", fill = "grey", alpha = 0.5) +
  theme_minimal() +
  labs(x = NULL, y = "Y", title = "Smoothed Curve of parameter b")

print(exp_b_plot_b_rw)
## `geom_smooth()` using formula = 'y ~ x'

fit model

out_b_rw_FR <-
  nowcasting_moving_window(b_rw_FR$case_reported_cumulated, scoreRange = scoreRange,
                          case_true = b_rw_FR$case_true,
                          start_date = as.Date("2024-01-01"),
                          D = D, seeds = seed,
                          methods =models_to_use,
                          compiled_models = compiled_models,
                          iter_sampling = 2000, iter_warmup = 1000, refresh = 0,
                          num_chains = 3, suppress_output = T,
                          posterior_draws_path = file.path(posterior_draws_path, "b_rw")
                          )
## ====================
## now=2024-01-10 (1/5)
## ====================
## Warning in nowcasting_moving_window(b_rw_FR$case_reported_cumulated, scoreRange
## = scoreRange, : The number of rows of the input data is smaller than number of
## max delay D, which might cause inaccuracy.
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 0.6 seconds.
## Chain 2 finished in 0.5 seconds.
## Chain 3 finished in 0.5 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 0.6 seconds.
## Total execution time: 2.2 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 0.2 seconds.
## Chain 2 finished in 0.3 seconds.
## Chain 3 finished in 0.2 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 0.2 seconds.
## Total execution time: 1.1 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.3 seconds.
## Chain 2 finished in 2.2 seconds.
## Chain 3 finished in 1.7 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 1.7 seconds.
## Total execution time: 5.5 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 2.3 seconds.
## Chain 2 finished in 3.1 seconds.
## Chain 3 finished in 2.4 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 2.6 seconds.
## Total execution time: 8.2 seconds.
## 
## ====================
## now=2024-01-20 (2/5)
## ====================
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.3 seconds.
## Chain 2 finished in 1.2 seconds.
## Chain 3 finished in 1.2 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 1.2 seconds.
## Total execution time: 4.0 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 0.4 seconds.
## Chain 2 finished in 0.4 seconds.
## Chain 3 finished in 0.3 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 0.4 seconds.
## Total execution time: 1.4 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 3.3 seconds.
## Chain 2 finished in 4.6 seconds.
## Chain 3 finished in 3.6 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 3.8 seconds.
## Total execution time: 11.7 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 4.3 seconds.
## Chain 2 finished in 4.0 seconds.
## Chain 3 finished in 3.7 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 4.0 seconds.
## Total execution time: 12.2 seconds.
## 
## ====================
## now=2024-01-30 (3/5)
## ====================
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.7 seconds.
## Chain 2 finished in 1.6 seconds.
## Chain 3 finished in 1.5 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 1.6 seconds.
## Total execution time: 5.1 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 0.7 seconds.
## Chain 2 finished in 0.6 seconds.
## Chain 3 finished in 0.5 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 0.6 seconds.
## Total execution time: 2.1 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 4.4 seconds.
## Chain 2 finished in 4.2 seconds.
## Chain 3 finished in 4.4 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 4.3 seconds.
## Total execution time: 13.3 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 3.7 seconds.
## Chain 2 finished in 4.4 seconds.
## Chain 3 finished in 3.8 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 4.0 seconds.
## Total execution time: 12.3 seconds.
## 
## ====================
## now=2024-02-09 (4/5)
## ====================
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 2.5 seconds.
## Chain 2 finished in 2.6 seconds.
## Chain 3 finished in 2.4 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 2.5 seconds.
## Total execution time: 7.7 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 0.7 seconds.
## Chain 2 finished in 0.7 seconds.
## Chain 3 finished in 0.7 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 0.7 seconds.
## Total execution time: 2.4 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 5.8 seconds.
## Chain 2 finished in 5.8 seconds.
## Chain 3 finished in 5.7 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 5.8 seconds.
## Total execution time: 17.7 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 4.7 seconds.
## Chain 2 finished in 4.7 seconds.
## Chain 3 finished in 5.4 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 4.9 seconds.
## Total execution time: 15.1 seconds.
## 
## ====================
## now=2024-02-19 (5/5)
## ====================
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 3.1 seconds.
## Chain 2 finished in 2.7 seconds.
## Chain 3 finished in 3.2 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 3.0 seconds.
## Total execution time: 9.2 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 0.9 seconds.
## Chain 2 finished in 0.9 seconds.
## Chain 3 finished in 0.9 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 0.9 seconds.
## Total execution time: 3.0 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 7.4 seconds.
## Chain 2 finished in 7.8 seconds.
## Chain 3 finished in 7.6 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 7.6 seconds.
## Total execution time: 23.1 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 7.3 seconds.
## Chain 2 finished in 6.4 seconds.
## Chain 3 finished in 6.6 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 6.7 seconds.
## Total execution time: 20.6 seconds.
save(out_b_rw_FR, file = file.path(data_save_path, "FR_b_rw.RData"))
#load( file.path(data_save_path, "FR_b_rw.RData"))

results_b_rw_FR <- nowcasts_table(out_b_rw_FR, D = D, report_unit = "day", 
                          methods = models_to_use)

plots_b_rw_FR <- nowcasts_plot(results_b_rw_FR, D = D, report_unit = "day", methods = models_to_use)
## Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
for (i in 1:length(results_b_rw_FR)) {
  print(calculate_metrics(results_b_rw_FR[[i]],
                          methods = models_to_use))
}
##    RMSE RMSPE   MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 77.76 62.00 53.53 60.54         120.61           0.6 q_constant
## 2 15.48  8.05  8.07  6.66          57.31           1.0 b_constant
## 3 16.19  8.09  8.20  6.71          67.21           1.0       b_rw
## 4 16.11  8.18  8.19  6.75          61.61           1.0       b_ou
##    RMSE RMSPE   MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 67.30 12.43 46.87 11.57         107.16          0.75 q_constant
## 2 31.90  4.50 15.75  3.07          80.65          1.00 b_constant
## 3 27.66  3.88 12.12  2.40          95.06          1.00       b_rw
## 4 32.36  4.49 15.30  2.93          90.51          1.00       b_ou
##    RMSE RMSPE   MAE MAPE Interval_Width Coverage_Rate     Method
## 1 54.52  4.65 34.19 3.75         109.34          0.87 q_constant
## 2 30.63  2.88 15.09 2.07         103.67          0.97 b_constant
## 3 22.12  1.91  8.14 1.12         124.34          1.00       b_rw
## 4 22.88  2.04  9.28 1.27         124.95          1.00       b_ou
##    RMSE RMSPE   MAE MAPE Interval_Width Coverage_Rate     Method
## 1 24.40  2.43 15.79 1.82         114.33             1 q_constant
## 2 17.62  2.33  9.94 1.59         112.18             1 b_constant
## 3 19.45  2.14  7.97 1.10         126.29             1       b_rw
## 4 13.36  1.71  6.42 1.02         125.13             1       b_ou
##    RMSE RMSPE   MAE MAPE Interval_Width Coverage_Rate     Method
## 1 33.60  6.90 15.32 2.61         110.07          0.98 q_constant
## 2 30.66  6.63 11.12 2.32         108.13          0.98 b_constant
## 3 15.13  3.31  5.80 1.24         112.65          1.00       b_rw
## 4 19.68  4.35  6.14 1.40         113.17          1.00       b_ou
for (i in 1:length(results_b_rw_FR)) {
  print(calculate_metrics(data.table::last(results_b_rw_FR[[i]],D_check),
                          methods = models_to_use))
}
##     RMSE RMSPE   MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 109.38 67.98 96.61 66.08         211.23           0.2 q_constant
## 2  21.86  9.17 15.36  8.42          97.01           1.0 b_constant
## 3  22.86  9.31 15.58  8.40         116.41           1.0       b_rw
## 4  22.75  9.33 15.60  8.43         105.81           1.0       b_ou
##     RMSE RMSPE    MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 117.55 15.88 109.96 14.80         226.63           0.2 q_constant
## 2  61.62  7.75  49.53  6.40         166.02           1.0 b_constant
## 3  53.39  6.65  37.18  4.76         219.22           1.0       b_rw
## 4  62.92  7.89  49.07  6.33         202.02           1.0       b_ou
##     RMSE RMSPE    MAE MAPE Interval_Width Coverage_Rate     Method
## 1 110.77  8.05 102.23 7.52         231.61           0.4 q_constant
## 2  68.07  4.85  58.24 4.25         220.60           0.8 b_constant
## 3  52.28  3.67  34.30 2.43         337.63           1.0       b_rw
## 4  54.72  3.83  42.74 3.04         339.24           1.0       b_ou
##    RMSE RMSPE   MAE MAPE Interval_Width Coverage_Rate     Method
## 1 46.65  4.58 35.68 3.39         198.42             1 q_constant
## 2 38.92  3.77 28.06 2.64         192.42             1 b_constant
## 3 53.08  5.24 39.78 3.83         293.43             1       b_rw
## 4 35.47  3.46 28.97 2.77         289.23             1       b_ou
##    RMSE RMSPE   MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 97.61 21.08 62.40 13.01         140.01           0.8 q_constant
## 2 93.36 20.19 56.80 11.93         137.80           0.8 b_constant
## 3 45.81  9.84 31.14  6.40         169.40           1.0       b_rw
## 4 61.00 13.19 38.42  7.95         175.02           1.0       b_ou
print(plots_b_rw_FR)
## [[1]]

## 
## [[2]]

## 
## [[3]]

## 
## [[4]]

## 
## [[5]]

out_b_rw_NFR <-
  nowcasting_moving_window(b_rw_NFR$case_reported_cumulated, scoreRange = scoreRange,
                          case_true = b_rw_NFR$case_true,
                          start_date = as.Date("2024-01-01"),
                          D = D, seeds = seed,
                          methods =models_to_use,
                          compiled_models = compiled_models,
                          iter_sampling = 2000, iter_warmup = 1000, refresh = 0,
                          num_chains = 3, suppress_output = T,
                          posterior_draws_path = file.path(posterior_draws_path, "b_rw")
                          )
## ====================
## now=2024-01-10 (1/5)
## ====================
## Warning in nowcasting_moving_window(b_rw_NFR$case_reported_cumulated,
## scoreRange = scoreRange, : The number of rows of the input data is smaller than
## number of max delay D, which might cause inaccuracy.
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 0.6 seconds.
## Chain 2 finished in 0.5 seconds.
## Chain 3 finished in 0.5 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 0.5 seconds.
## Total execution time: 1.8 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 0.2 seconds.
## Chain 2 finished in 0.2 seconds.
## Chain 3 finished in 0.2 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 0.2 seconds.
## Total execution time: 1.1 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.7 seconds.
## Chain 2 finished in 2.6 seconds.
## Chain 3 finished in 2.3 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 2.2 seconds.
## Total execution time: 7.0 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 2.2 seconds.
## Chain 2 finished in 2.6 seconds.
## Chain 3 finished in 1.3 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 2.0 seconds.
## Total execution time: 6.3 seconds.
## 
## ====================
## now=2024-01-20 (2/5)
## ====================
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.3 seconds.
## Chain 2 finished in 1.2 seconds.
## Chain 3 finished in 1.1 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 1.2 seconds.
## Total execution time: 3.9 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 0.8 seconds.
## Chain 2 finished in 0.7 seconds.
## Chain 3 finished in 0.8 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 0.8 seconds.
## Total execution time: 2.5 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 2.1 seconds.
## Chain 2 finished in 6.0 seconds.
## Chain 3 finished in 5.5 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 4.6 seconds.
## Total execution time: 14.0 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 4.7 seconds.
## Chain 2 finished in 4.0 seconds.
## Chain 3 finished in 5.3 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 4.7 seconds.
## Total execution time: 14.2 seconds.
## 
## ====================
## now=2024-01-30 (3/5)
## ====================
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.4 seconds.
## Chain 2 finished in 1.3 seconds.
## Chain 3 finished in 1.3 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 1.3 seconds.
## Total execution time: 4.3 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.3 seconds.
## Chain 2 finished in 1.5 seconds.
## Chain 3 finished in 1.3 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 1.4 seconds.
## Total execution time: 4.4 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 9.8 seconds.
## Chain 2 finished in 11.0 seconds.
## Chain 3 finished in 9.7 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 10.2 seconds.
## Total execution time: 30.8 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 7.1 seconds.
## Chain 2 finished in 16.7 seconds.
## Chain 3 finished in 9.3 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 11.0 seconds.
## Total execution time: 33.3 seconds.
## 
## ====================
## now=2024-02-09 (4/5)
## ====================
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 2.1 seconds.
## Chain 2 finished in 1.9 seconds.
## Chain 3 finished in 1.7 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 1.9 seconds.
## Total execution time: 6.1 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.9 seconds.
## Chain 2 finished in 1.3 seconds.
## Chain 3 finished in 1.2 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 1.5 seconds.
## Total execution time: 4.7 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 13.4 seconds.
## Chain 2 finished in 11.5 seconds.
## Chain 3 finished in 13.8 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 12.9 seconds.
## Total execution time: 39.0 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 9.6 seconds.
## Chain 2 finished in 7.9 seconds.
## Chain 3 finished in 7.4 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 8.3 seconds.
## Total execution time: 25.1 seconds.
## 
## ====================
## now=2024-02-19 (5/5)
## ====================
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 2.1 seconds.
## Chain 2 finished in 2.0 seconds.
## Chain 3 finished in 1.9 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 2.0 seconds.
## Total execution time: 6.3 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 1.8 seconds.
## Chain 2 finished in 1.6 seconds.
## Chain 3 finished in 2.8 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 2.0 seconds.
## Total execution time: 6.5 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 26.3 seconds.
## Chain 2 finished in 17.4 seconds.
## Chain 3 finished in 19.5 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 21.1 seconds.
## Total execution time: 63.5 seconds.
## 
## Running MCMC with 3 sequential chains...
## 
## Chain 1 finished in 13.2 seconds.
## Chain 2 finished in 9.8 seconds.
## Chain 3 finished in 12.2 seconds.
## 
## All 3 chains finished successfully.
## Mean chain execution time: 11.7 seconds.
## Total execution time: 35.6 seconds.
save(out_b_rw_NFR, file = file.path(data_save_path, "FR_b_rw.RData"))
#load( file.path(data_save_path, "FR_b_rw.RData"))

results_b_rw_NFR <- nowcasts_table(out_b_rw_NFR, D = D, report_unit = "day", 
                          methods = models_to_use)

plots_b_rw_NFR <- nowcasts_plot(results_b_rw_NFR, D = D, report_unit = "day", methods = models_to_use)

for (i in 1:length(results_b_rw_NFR)) {
  print(calculate_metrics(results_b_rw_NFR[[i]],
                          methods = models_to_use))
}
##    RMSE RMSPE   MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 14.16 16.62  8.58 11.47          93.01           1.0 q_constant
## 2 64.62 47.77 45.08 46.96          50.71           0.3 b_constant
## 3 68.84 49.35 47.62 48.51          55.21           0.2       b_rw
## 4 67.94 49.48 47.24 48.69          51.41           0.2       b_ou
##    RMSE RMSPE   MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 66.32 19.06 46.68 16.43         108.71          0.65 q_constant
## 2 57.70 12.68 32.79  8.96         126.31          0.95 b_constant
## 3 41.77 14.66 26.41 10.79         200.06          0.95       b_rw
## 4 57.99 14.73 35.33 10.81         166.76          0.95       b_ou
##     RMSE RMSPE   MAE  MAPE Interval_Width Coverage_Rate     Method
## 1  94.94 16.79 66.40 13.93         110.17          0.53 q_constant
## 2 144.04 15.96 96.20 13.46         150.02          0.63 b_constant
## 3 113.92 12.26 57.00  8.11         234.92          0.97       b_rw
## 4  68.15  9.78 41.96  6.93         213.45          1.00       b_ou
##     RMSE RMSPE    MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 166.19 19.54 132.10 18.04         109.43          0.20 q_constant
## 2  63.96  9.37  47.07  6.86         126.53          0.82 b_constant
## 3  67.79  9.13  45.02  6.35         198.72          0.98       b_rw
## 4  58.65  8.39  37.98  5.69         181.21          0.98       b_ou
##     RMSE RMSPE    MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 155.69 18.48 125.62 17.15         102.29          0.20 q_constant
## 2  49.65  8.36  35.78  5.90         117.85          0.80 b_constant
## 3  35.97  7.46  25.93  4.73         158.80          0.98       b_rw
## 4  36.47  6.97  26.47  4.53         153.99          0.98       b_ou
for (i in 1:length(results_b_rw_NFR)) {
  print(calculate_metrics(data.table::last(results_b_rw_NFR[[i]],D_check),
                          methods = models_to_use))
}
##    RMSE RMSPE   MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 19.59  9.98 14.03  7.69         159.42           1.0 q_constant
## 2 90.71 47.77 79.36 47.61          84.81           0.2 b_constant
## 3 96.71 50.55 84.40 50.40          93.82           0.0       b_rw
## 4 95.42 50.19 83.46 50.05          86.41           0.0       b_ou
##    RMSE RMSPE   MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 79.97 11.55 68.84  9.75         243.82           0.6 q_constant
## 2 97.91 12.01 78.80 10.02         283.42           1.0 b_constant
## 3 45.17  6.06 37.79  5.11         546.41           1.0       b_rw
## 4 77.77 11.18 64.56  9.20         423.63           1.0       b_ou
##     RMSE RMSPE    MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 172.73 12.55 151.99 11.08         238.83           0.2 q_constant
## 2 202.00 15.91 179.99 13.82         326.43           0.4 b_constant
## 3 267.52 19.45 241.98 17.71         655.44           0.8       b_rw
## 4 151.98 11.06 138.26 10.18         586.63           1.0       b_ou
##     RMSE RMSPE    MAE  MAPE Interval_Width Coverage_Rate     Method
## 1 141.15 12.43 134.23 11.84         204.00           0.4 q_constant
## 2  72.05  6.35  67.93  5.99         236.82           1.0 b_constant
## 3  88.39  8.08  79.34  7.19         518.02           1.0       b_rw
## 4  55.66  4.99  42.24  3.79         434.85           1.0       b_ou
##    RMSE RMSPE   MAE MAPE Interval_Width Coverage_Rate     Method
## 1 48.31  9.79 45.40 9.45         126.20           0.8 q_constant
## 2 46.17  9.88 43.76 9.40         149.01           1.0 b_constant
## 3 17.62  4.03 15.39 3.43         249.83           1.0       b_rw
## 4 14.10  2.94 11.04 2.29         215.83           1.0       b_ou
print(plots_b_rw_NFR)
## [[1]]

## 
## [[2]]

## 
## [[3]]

## 
## [[4]]

## 
## [[5]]